import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2025-01-19
 * Time: 12:07
 */
public class test1 {
    /*
    LeetCode 438 找到字符串中所有字母异位词
     */
    //用哈希记录abc

    class Solution
    {
        public List<Integer> findAnagrams(String ss, String pp) {
            List<Integer> ret = new ArrayList<Integer>();
            char[] s = ss.toCharArray();
            char[] p = pp.toCharArray();
            int[] hash1 = new int[26]; // 统计字符串 p 中每⼀个字符出现的个数
            for(char ch : p){
                hash1[ch - 'a']++;
            }
            int[] hash2 = new int[26]; // 统计窗⼝中每⼀个字符出现的个数
            int m = p.length;
            for(int left = 0, right = 0, count = 0; right < s.length; right++){
                char in = s[right];
                // 进窗⼝ + 维护 count
                if(++hash2[in - 'a'] <= hash1[in - 'a']) count++;
                if(right - left + 1 > m) // 判断
                {
                    char out = s[left++];
                    // 出窗⼝ + 维护 count
                    if(hash2[out - 'a']-- <= hash1[out - 'a']) count--;
                }
                // 更新结果
                if(count == m) ret.add(left);
            }
            return ret;
        }
    }
/*
class Solution{
    public List<Integer> findAnagrams(String ss, String pp) {
        List<Integer> ret=new ArrayList<Integer>();
        // 1. 统计字符串p中出现的字符
        char [] s =ss.toCharArray();
        char [] p =pp.toCharArray();
        int [] hash1 = new int [26];
        for(char ch : p){
            hash1[ch -'a']++;
        }
        //统计字符串s出现的字符
        int [] hash2 =new int[26];
        int m=p.length;
        for(int right=0,left=0,count=0;right<s.length;right++){
            char in = s[right];
            hash2[in-'a']++;
            if(hash2[in - 'a']<=hash1[in - 'a']){
                count++;
            }
            if(right-left+1>m){
                char out =s[left];
                left++;
                if(hash2[out - 'a'] <= hash1[out-'a']){
                    count--;
                }
                hash2[out - 'a']--;
            }
            if(count == m){
                ret.add(left);
            }
        }
        return ret;
    }
}
*/
}
